package com.info.game;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.component.game.RenderComponent;
import com.engine.game.GameObject;
import com.engine.game.Helper;
import com.engine.game.MessageInfo;

public class GravityRobotRenderComponent implements RenderComponent{

	
	private Sprite sprite;
	
	
	private Animation walking;
	private Animation standing;
	private Animation power;
	
	
	private Texture walkingTexture;
	private Texture standingTexture;
	private Texture powerTexture;
	
	private TextureRegion[] walkingFrames;
	private TextureRegion[] standingFrames;
	private TextureRegion[] powerFrames;
	
	
	
	private int WALKING_ROWS = 2;
	private int WALKING_COLS = 7;
	private int WALKING_FRAMES = 12;
	
	private int STANDING_ROWS = 2;
	private int STANDING_COLS = 5;
	private int STANDING_FRAMES = 8;
	
	
	private int POWER_ROWS = 2;
	private int POWER_COLS = 8;
	private int POWER_FRAMES = 11;
	
	
	private float stateTime = 0.0f;
	
	private boolean rightDirection = true;
	private boolean inverse = false;
	
	private enum AnimationState{
		WALKING,
		STANDING,
		POWER;
	}
	
	private AnimationState currentAnimation ;
	
	public GravityRobotRenderComponent(){
		
		this.currentAnimation = AnimationState.STANDING;
		
		TextureRegion[][] tmp;
		int index = 0;
		
       // this.sprite = new Sprite();
		this.sprite = new Sprite(new Texture("images/start_button.png"));
        
        this.sprite.setBounds(Helper.subHalf(20, 4), Helper.subHalf(15, 6), 4, 6);
        
        this.walkingTexture = new Texture("images/gravity_robot_walking.png");
        tmp = TextureRegion.split(this.walkingTexture, this.walkingTexture.getWidth()/WALKING_COLS, this.walkingTexture.getHeight()/WALKING_ROWS);
        this.walkingFrames = new TextureRegion[WALKING_FRAMES];
        
        for (int i = 0; i < WALKING_ROWS; i++) {
            for (int j = 0; j < WALKING_COLS; j++) {
            	
            	
            	
            	if(index > (WALKING_FRAMES-1)){
            		break;
            	}
            	
            	this.walkingFrames[index] = tmp[i][j];
            	
            	index++;
            }
            
            if(index > (WALKING_FRAMES-1)){
        		break;
        	}
        }
        
        this.walking = new Animation(0.5f, this.walkingFrames);
        
        
        
        index = 0;
        this.standingTexture = new Texture("images/gravity_robot_standing.png");
        tmp = TextureRegion.split(this.standingTexture, this.standingTexture.getWidth()/STANDING_COLS, this.standingTexture.getHeight()/STANDING_ROWS);
        this.standingFrames = new TextureRegion[STANDING_FRAMES];
        
        for (int i = 0; i < STANDING_ROWS; i++) {
            for (int j = 0; j < STANDING_COLS; j++) {
            	
            	
            	
            	if(index > (STANDING_FRAMES-1)){
            		break;
            	}
            	
            	this.standingFrames[index] = tmp[i][j];
            	
            	index++;
            }
            
            if(index > (STANDING_FRAMES-1)){
        		break;
        	}
        }
        
        this.standing = new Animation(0.1f, this.standingFrames);
        
        
        
        
        
        
        index = 0;
        this.powerTexture = new Texture("images/gravity_robot_power.png");
        tmp = TextureRegion.split(this.powerTexture, this.powerTexture.getWidth()/POWER_COLS, this.powerTexture.getHeight()/POWER_ROWS);
        this.powerFrames = new TextureRegion[POWER_FRAMES];
        
        for (int i = 0; i < POWER_ROWS; i++) {
            for (int j = 0; j < POWER_COLS; j++) {
            	
            	
            	
            	if(index > (POWER_FRAMES-1)){
            		break;
            	}
            	
            	this.powerFrames[index] = tmp[i][j];
            	
            	index++;
            }
            
            if(index > (POWER_FRAMES-1)){
        		break;
        	}
        }
        
        this.power = new Animation(0.5f, this.powerFrames);
        
	}
	
	
	@Override
	public void dispose() {
		this.walkingTexture.dispose();
		this.standingTexture.dispose();
		this.powerTexture.dispose();
		
	}

	@Override
	public void update(GameObject object) {
		
		
		
		
		this.sprite.setPosition(Helper.subHalf(object.getMove().getCenterX(), 4), Helper.subHalf(object.getMove().getCenterY(), 6));
		
		
		
		for(MessageInfo message : object.getRenderMessages()){
			
			if(message.message.contains("walk_left")){
				this.stateTime = 0.0f;
				this.currentAnimation = AnimationState.WALKING;
				this.rightDirection = false;
				
				
			}
			else if(message.message.contains("walk_right")){
				this.stateTime = 0.0f;
				this.currentAnimation = AnimationState.WALKING;
				this.rightDirection = true;
			}
			else if(message.message.contains("inverse")){
				
				this.inverse = !this.inverse;
			}
			else if(message.message.contains("stop")){
				this.stateTime = 0.0f;
				this.currentAnimation = AnimationState.STANDING;
				
			}
			else if(message.message.contains("power")){
				this.stateTime = 0.0f;
				this.currentAnimation = AnimationState.POWER;
				
			}
			
		}
		
	}

	@Override
	public Sprite getSprite(){
		
		this.sprite.setRegion(this.chooseFrame());
		
		//this.sprite = new Sprite(this.chooseFrame());
		//this.sprite.setSize(SizeOfThings.TimeRobotWidth, SizeOfThings.TimeRobotHeight);
		
		
		
		if(this.inverse){
			this.sprite.flip(false, true);
		}
		
		if(!this.rightDirection){
			this.sprite.flip(true, false);
		}
		
		
		
		return this.sprite;
	}
		
		
	private TextureRegion chooseFrame(){
		
		this.stateTime += Gdx.graphics.getDeltaTime();
		
		switch(this.currentAnimation){
		
		case POWER:
			if(this.stateTime > this.power.getAnimationDuration()){
				this.stateTime = 0.0f;
			}
			return this.power.getKeyFrame(stateTime, false);
		case STANDING:
			if(this.stateTime > this.standing.getAnimationDuration()){
				this.stateTime = 0.0f;
			}
			return this.standing.getKeyFrame(stateTime, true);
		case WALKING:
			if(this.stateTime > this.walking.getAnimationDuration()){
				this.stateTime = 0.0f;
			}
			return this.walking.getKeyFrame(stateTime, true);
		default:
			return this.standing.getKeyFrame(stateTime, true);
			
		
		}
		
		
		
	}
	

}
